<template>
    <view class="my">
        <image mode="widthFix" src="../../static/map.jpg" style="width: 100%;"/>
        <uni-section title="物流信息" type="line" padding>
            <uni-steps :options="data.list2" active-color="#007AFF" :active="data.active" direction="column" />
        </uni-section>
        <button v-if="data.title == '确认收货'" type="primary" plain="true" @click="change">确认收货</button>
        <button v-else type="primary" plain="true" @click="change">返回</button>
        <uni-popup ref="alertDialog" type="dialog">
			<uni-popup-dialog type="warn" cancelText="关闭" confirmText="同意" title="是否确认收货!" @confirm="dialogConfirm"
				@close="dialogClose"></uni-popup-dialog>
		</uni-popup>
    </view>
</template>


<script setup lang="ts">
import { onLoad } from '@dcloudio/uni-app'
import { ref, reactive, toRefs } from 'vue'
import {productOrderIdApi, productOrderPutApi} from '../../https/http'
const alertDialog = ref()
const acitem = ref()
const data = reactive<{
    id?: number
    active: number
    title:string
    list2: {
        title: string
        desc: string
    }[]
}>({
    id: undefined,
    active: 2,
    title: "确认收货",
    list2: [{
        title: '买家下单',
        desc: new Date(new Date().getTime() - (3 * 3600 * 24 * 1000)).toLocaleDateString()
    }, {
        title: '卖家发货',
        desc: new Date(new Date().getTime() - (2 * 3600 * 24 * 1000)).toLocaleDateString()
    }, {
        title: '快递揽收',
        desc: new Date(new Date().getTime() - (1 * 3600 * 24 * 1000)).toLocaleDateString()
    }, {
        title: '买家签收',
        desc: "..."
    }, {
        title: '交易完成',
        desc: "..."
    }]

})

const change = (item:any) => {
    if (data.title == "确认收货") {
	    alertDialog.value.open()
    } else {
        uni.navigateBack({
            delta: 1
        })
    }

}
const dialogConfirm = async () => {
	const datas = {
		product_order_id: acitem.value.product_order_id,
		product_order_state: 3
	}
	try {
		const res = await productOrderPutApi(datas)
		if (res.code == 200) {
            uni.navigateBack({
                delta: 1
            })
		}
	} catch (error) {
		console.log(error);
	}

}
const dialogClose = () => {
	console.log("关闭");
}
onLoad((e:any) => {
    data.id = e.id
    newData()
})
const newData = async() => {
    try {
        const res:any = await productOrderIdApi(data.id as number)
        if (res.code === 200) {
            acitem.value = res.data
            switch (res.data.product_order_state) {
                case 2:
                    data.active = 2
                    break;
                case 3:
                    data.active = 4
                    data.list2[3].desc = new Date(new Date().getTime() - (0.5 * 3600 * 24 * 1000)).toLocaleDateString()
                    data.list2[4].desc = new Date(new Date().getTime()).toLocaleDateString()
                    data.title = "返回"
                    break;
                default:
                    data.active = 3
                    break;
            }
        }

    } catch (error) {
        console.log(error);
    }

}
</script>

<style scoped></style>
